/* eslint-disable no-param-reassign */
/* eslint-disable array-callback-return */
import { message } from 'antd';
import {
  getCustBillInvList, // 客服系统 - 客户开票 - 账单开票
  getSuppBillInvList, // 客服系统 - 供应商开票 - 账单开票
  getSupplierInvList, // 供应商系统 - 开票申请 - 账单开票
  getCustOnceInvList, //  客服系统 - 客户开票 - 一次性开票
  getSuppOnceInvList, //  客服系统 - 客户开票 - 一次性开票
  getSupplierOnceInvList, //  客服系统 - 客户开票 - 一次性开票
  getInsDetailData, // 发票明细 - 详情
  addSubmitData, // 新增 - 发票明细
  updateSubmitData, // 修改 - 发票明细
  featchFinishId, // 提交数据 - 到完成页面
  getExpensesDetail, // 完善发票信息 - 费用信息（上半部分）
  getPdListData, // 获取产品下拉
  deleteRecordData, // 删除发票明细
  getInvoiceRateList, // 获取开票项目和税率
  getFinishDataList, // 获取结果页数据
  fetchSuppDataList, // 获取供应商下拉
} from '@/services/invbycustsupp';

export default {
  state: {
    // 账单开票列表
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    // 一次性开票列表
    oncePageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    accuData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    isGoBack: false,
    formFieldsValue: {
      costType: '1',
    },
    recordData: {},
    expensesData: {
      invApplyDto: {
        invApplyBillOptVo: [],
      },
    },
    selectedRows: [], // 选中的
    selectedRowKeys: [], // 选中的
  },

  effects: {
    // 客服系统 - 客户开票 - 账单开票
    *customerCustBillInv({ payload }, { call, put }) {
      const response = yield call(getCustBillInvList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getInvDataList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        // eslint-disable-next-line no-unused-expressions
        response.data &&
          response.data.pageRecords &&
          response.data.pageRecords.map((item, index) => {
            item.key = `${currentPage}@${index}` || null;
          });
        yield put({
          type: 'getInvDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getInvDataList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },

    // 客服系统 - 供应商开票 - 账单开票
    *customerSuppBillInv({ payload }, { call, put }) {
      const response = yield call(getSuppBillInvList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getInvDataList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        // eslint-disable-next-line no-unused-expressions
        response.data &&
          response.data.pageRecords &&
          response.data.pageRecords.map((item, index) => {
            item.key = `${currentPage}@${index}` || null;
          });
        yield put({
          type: 'getInvDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getInvDataList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },

    // 供应商系统 - 开票申请 - 账单开票
    *supplierSuppBillInv({ payload }, { call, put }) {
      const response = yield call(getSupplierInvList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        // eslint-disable-next-line no-unused-expressions
        response.data &&
          response.data.pageRecords &&
          response.data.pageRecords.map((item, index) => {
            item.key = `${currentPage}@${index}` || null;
          });
        yield put({
          type: 'getInvDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getInvDataList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },

    // 客服系统 - 客户开票 - 一次性开票
    *customerOnceBillInv({ payload }, { call, put }) {
      const response = yield call(getCustOnceInvList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getOnceInvDataList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        // eslint-disable-next-line no-unused-expressions
        response.data &&
          response.data.pageRecords &&
          response.data.pageRecords.map((item, index) => {
            item.key = `${currentPage}@${index}` || null;
          });
        yield put({
          type: 'getOnceInvDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getOnceInvDataList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },

    // 客服系统 - 供应商开票 - 一次性开票
    *customerSuppOnceBillInv({ payload }, { call, put }) {
      const response = yield call(getSuppOnceInvList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getOnceInvDataList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        // eslint-disable-next-line no-unused-expressions
        response.data &&
          response.data.pageRecords &&
          response.data.pageRecords.map((item, index) => {
            item.key = `${currentPage}@${index}` || null;
          });
        yield put({
          type: 'getOnceInvDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getOnceInvDataList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
        message.warning(response.message || '获取列表失败：服务端未知错误');
      }
    },

    // 供应商系统 - 开票申请 - 一次性开票
    *supplierOnceBillInv({ payload }, { call, put }) {
      const response = yield call(getSupplierOnceInvList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getOnceInvDataList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        // eslint-disable-next-line no-unused-expressions
        response.data &&
          response.data.pageRecords &&
          response.data.pageRecords.map((item, index) => {
            item.key = `${currentPage}@${index}` || null;
          });
        yield put({
          type: 'getOnceInvDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getOnceInvDataList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
        message.warning(response.message || '获取列表失败：服务端未知错误');
      }
    },

    // 发票明细查看详情
    *getDetailData({ payload }, { call }) {
      const response = yield call(getInsDetailData, payload);
      return response;
    },

    // 新增发票明细
    *addInvData({ payload }, { call }) {
      const response = yield call(addSubmitData, payload);
      return response;
    },
    // 修改发票明细
    *updateInvData({ payload }, { call }) {
      const response = yield call(updateSubmitData, payload);
      return response;
    },

    // 提交数据 - 跳转完成页
    *getFinishId({ payload }, { call }) {
      const response = yield call(featchFinishId, payload);
      return response;
    },

    // 获取产品下拉
    *getPdList({ payload }, { call }) {
      const response = yield call(getPdListData, payload);
      return response;
    },

    // 完善发票信息上半部分
    *expensesDetail({ payload }, { call, put }) {
      const response = yield call(getExpensesDetail, payload);
      let invMoney = 0;
      if (response.success) {
        if (
          response.data &&
          response.data.invApplyDto &&
          response.data.invApplyDto.invApplyBillOptVo
        ) {
          response.data.invApplyDto.invApplyBillOptVo.map((item, index) => {
            if (item.invTerm && item.invTerm.length) {
              item.invTerm.map(mitem => {
                invMoney += mitem.invAmount;
              });
            }
            // eslint-disable-next-line no-param-reassign
            item.dataInfo = {
              number: index + 1,
              ...item.invCompany,
              ...item.invMail,
              ...item.invRule,
              invTerm: item.invTerm,
              invTermMoney: item.invTerm,
            };
          });
          const newInvApplyBillOptVo = [];
          response.data.invApplyDto.invApplyBillOptVo.map(item => {
            newInvApplyBillOptVo.push(item.dataInfo);
          });
          response.data.invApplyDto.invMoney = invMoney;
          response.data.invApplyDto.invApplyBillOptVo = newInvApplyBillOptVo;
          let amountNoInv = null;
          amountNoInv = (response.data && response.data.billAmo) - invMoney;
          response.data.amountNoInv = amountNoInv;
        } else {
          response.data.amountNoInv = response.data.billAmo;
        }
        yield put({
          type: 'expensesDetailData',
          payload: response.data,
        });
      } else {
        yield put({
          type: 'expensesDetailData',
          payload: {},
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },

    // 删除发票明细数据
    *deleteRecord({ payload }, { call }) {
      const response = yield call(deleteRecordData, payload);
      return response;
    },

    // 获取开票项目和税率
    *getInvoiceRate({ payload }, { call }) {
      const response = yield call(getInvoiceRateList, payload);
      return response;
    },

    // 获取结果页数据
    *getFinishData({ payload }, { call }) {
      const response = yield call(getFinishDataList, payload);
      return response;
    },

    // 获取供应商下拉
    *fetchSuppList({ payload, callback }, { call }) {
      const response = yield call(fetchSuppDataList, payload);
      if (callback) {
        callback(response);
      }
    },
  },

  reducers: {
    // 获取账单开票列表
    getInvDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    // 获取一次性开票列表
    getOnceInvDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        oncePageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    getAccuList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        accuData: { list, pagination: { total: data.totalCount, current: currentPage, pageSize } },
      };
    },
    getInsList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageDatas: { list, pagination: { total: data.totalCount, current: currentPage, pageSize } },
      };
    },
    getInsEmpNameInfo(state, { payload }) {
      return {
        ...state,
        insEmpNameInfo: payload,
      };
    },

    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 费用明细
    expensesDetailData(state, { payload }) {
      return {
        ...state,
        expensesData: payload,
      };
    },
  },
};
